<!-- 角色编辑弹窗 -->
<template>
  <div class="drawerDetail">
    <el-drawer :visible="drawerEditBoolen"
               size="50%"
               v-loading="loading"
               :before-close="handleClose"
               ref="drawer"
               :with-header="false">
      <!-- 订单信息 -->
      <el-card class="box-card"
               shadow="never">
        <div slot="header"
             class="clearfix">
          <span>基本信息</span>
        </div>
        <div class="">
          <div style="padding: 10px 0;">发布者: {{ orderInfoData?.nickname }}</div>
          <div style="padding: 10px 0;">发布内容: <span style="color: red;">{{orderInfoData?.title}}</span> </div>

          <div style="padding: 10px 0;">发布的照片:
            <div class="flex-start-center-wrap"
                 v-if="orderInfoData?.img.length">
              <div v-for="(item,index) in orderInfoData?.img"
                   :key="index">
                <img @click="imgScare(item)"
                     class="lifeImg"
                     :src="item"
                     alt="">
              </div>

            </div>
            <span style="padding: 10px 0;"
                  v-else>无照片</span>

          </div>
          <div style="padding: 10px 0;">点赞数量: {{orderInfoData?.like_number}}</div>
          <div style="padding: 10px 0;"> 评论数 {{ orderInfoData?.comment_number}} </div>

          <div style="padding: 10px 0;">活动地点: {{ orderInfoData?.address}}</div>

          <div style="padding: 10px 0;">当前状态:{{orderStatusFn(orderInfoData?.status)}}</div>

        </div>
      </el-card>

      <el-card class="box-card"
               shadow="never">
        <div slot="header"
             class="clearfix">
          <span>状态更改</span>

          <span style="color: red;font-size: 12px; margin-left: 12px;">如果需要进行状态更改请做以下操作,否则不要轻易进行更改</span>
        </div>

        <el-radio-group v-model="radio"
                        @input="inputChange">
          <el-radio :label="2"
                    v-if="orderInfoData?.status==1">审核通过</el-radio>

          <el-radio :label="3"
                    v-if="orderInfoData?.status==3||orderInfoData?.status==1">驳回</el-radio>

        </el-radio-group>
        <div>
          <el-input v-if="showTextarea"
                    :disabled="orderInfoData.status==3?true:false"
                    type="textarea"
                    resize="none"
                    placeholder="请输入内容"
                    v-model="remark">
          </el-input>
        </div>
      </el-card>
      <div class="drawer__footer"
           v-if="orderInfoData?.status==1"
           :style="{marginTop:!(radio==3)?'100px':''}">
        <el-button round
                   type="plain"
                   @click="handleClose"
                   size="medium">返回</el-button>
        <el-button type="primary"
                   round
                   :loading="loading"
                   size="medium"
                   @click="save">确定</el-button>
      </div>
      <!-- 订单信息 -->
      <el-card class="box-card"
               v-if="treeComentList.length"
               shadow="never">
        <div slot="header"
             class="clearfix">
          <span>评论列表</span>
        </div>
        <div v-for="(item,index) in treeComentList"
             :key="index">
          <div class="flex-between-top">
            <div class="allComments flex-between-start">
              <img class="commentator"
                   :src="item.comment_user_avatar"
                   alt="">

              <div class=""
                   style="width:450px;">
                <span style="color: rgb(153, 153, 153);">{{item.comment_user_nickname}}</span>
                <div style="margin-top: 7px;"
                     class="">
                  <span>{{item.content}}</span>

                </div>
                <div style="margin-top: 10px;">
                  <span style="color: rgb(153, 153, 153);">{{item.create_time}}</span> <span style="margin-left: 5px;">回复</span>
                </div>
                <div style="margin-top: 15px;"
                     class="">
                  <!--  -->
                  <div class=""
                       v-for="(items,i) in item.children"
                       :key="i">
                    <div class="flex-between-start"
                         style="margin-bottom: 10px;">

                      <img class="respondent"
                           :src="items.comment_user_avatar"
                           alt="">
                      <div class=""
                           style="width:330px;">
                        <div style="margin-top: 7px;">
                          <span style="color: rgb(153, 153, 153);">{{items.comment_user_nickname}}</span>
                        </div>
                        <div style="margin-top: 10px;">
                          回复<span style="color: rgb(153, 153, 153);margin: 0 5px;">{{items.reply_user_nickname }}</span>
                          <span>{{items.content}}</span>
                        </div>

                      </div>
                      <div class="flex-start-center"
                           style="min-width:50px;">

                        <i class="el-icon-star-off"
                           :style="{ 'color': items?.is_like == 1 ? '#fba1ab' : '#999999' }"></i>

                        <span style="color: rgb(153, 153, 153); margin-left: 5px;">{{items.like_number}}</span>
                      </div>
                    </div>

                    <div style="margin: 10 0;"
                         @click="reply(items.nickname,items.id)">
                      <span style="color: rgb(153, 153, 153);">{{items.create_time}}</span> <span style="margin-left: 5px;">回复</span>
                    </div>
                  </div>

                </div>
              </div>
              <div class="flex-start-center"
                   style="margin-right:7px;">

                <i class="el-icon-star-off"
                   :style="{ 'color': items?.is_like == 1 ? '#fba1ab' : '#999999' }"></i>
                <!-- <image class="share" src="../../static/第一行所有图标/组合 768.png" mode=""></image> -->
                <span style="color: rgb(153, 153, 153);margin-left: 5px;">{{item.like_number}}</span>
              </div>
              <!-- <image class="likeImg" src="../../static/第一行所有图标/组合 768.png" mode=""></image> -->
            </div>
            <el-popconfirm title="这是一段内容确定删除吗？"
                           @confirm="removeComment(item)">

              <div slot="reference"
                   style="color: red;"> 删除</div>
            </el-popconfirm>

          </div>

        </div>
      </el-card>
    </el-drawer>
    <!-- 图片预览 -->
    <el-image-viewer v-if="viewerVisible"
                     style="z-index: 9999"
                     :on-close="closeViewer"
                     :url-list="imgViewArr" />
    <el-button type="text"></el-button>
  </div>

</template>

<script>
import ElImageViewer from 'element-ui/packages/image/src/image-viewer';

// import {
//   userOssSignature,
//   qnyOssSignature
// } from '@/api/creatCenter/material';
// import { storageConfigDetail } from '@/api/storageInfo/index';

// import axios from 'axios';
// import {
//   siteCategoriesInsert,
//   siteCategoriesList,
//   siteCategoriesModify,
//   siteCategoriesDetail
// } from '@/api/serviceManagement/categoriesList';
import { upload, delComment } from '@/api/saveImg/index'



import { circleInfo, commentList, updateCircleStatus } from '@/api/dynamicManagement/index'
export default {
  name: 'pictureEdit',
  components: { ElImageViewer },
  props: {
    drawerEditBoolen: {
      type: Boolean,
      default: false
    },
    // 修改回显的数据
    data: Object
  },
  data () {
    // const categoryImage = (rule, value, callback) => {
    //   console.log(this.form.img);
    //   if (!this.form.img) {
    //     callback(new Error('请上传图片'));
    //   } else {
    //     callback();
    //   }
    // };
    const DEFAULT_FORM = {
      // 等级名称
      name: '',
      //   描述
      desc: '',
      // 0 普通会员  1 vip会员
      level: '1',
      //  价格
      price: '',
      // 有效期
      effective_time: '',
      // 搭子数量
      dazi_number: '',
      // 圈子数量
      circle_number: '',
      // 服务数量
      service_number: '',
      // 社群数量
      community_number: ''


    };
    return {
      radio: 2,
      storageMode: 2,
      options: [],
      imageUrl: '',
      imgViewArr: [],
      viewerVisible: false,
      // 表单数据
      form: { ...DEFAULT_FORM },
      dialogImageUrl: '',
      fileList: [],
      dialogVisible: false,
      // 表单验证规则
      rules: {
        name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ],


        desc: [
          {
            required: true,
            message: '请输入描述',
            trigger: 'blur'
          }
        ],
        price: [
          {
            required: true,
            message: '请输入价格',
            trigger: 'blur'
          }
        ],
        effective_time: [
          {
            required: true,
            message: '请输入有效时间',
            trigger: 'blur'
          }
        ],
        dazi_number: [
          {
            required: true,
            message: '请输入搭子数量',
            trigger: 'blur'
          }
        ],
        circle_number: [
          {
            required: true,
            message: '请输入圈子数量',
            trigger: 'blur'
          }
        ],
        service_number: [
          {
            required: true,
            message: '请输入服务数量',
            trigger: 'blur'
          }
        ],
        community_number: [
          {
            required: true,
            message: '请输入社群数量',
            trigger: 'blur'
          }
        ],
        // // 站点 图片上传
        // img: [
        //   { required: true, validator: categoryImage, trigger: 'change' }
        // ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      showChoose: false,
      orderInfoData: null,
      showTextarea: false,
      textarea: '',
      remark: '',
      status: 2,
      treeComentList: []
    };
  },
  activated () {

  },
  computed: {
    orderStatusFn: function () {
      return function (status) {

        let orderStatus = '';
        // status订单状态（0.待付款 1.待接单 2.已接单 3.服务中 4.已完成 5.已取消/退款)
        orderStatus =
          status == 0 ? '全部' : status == 1 ? '待审核' : status == 2 ? '审核通过' : '驳回';

        return orderStatus;
      };
    },

  },
  methods: {
    imgScare (imgStr) {

      console.log(imgStr);
      if (imgStr.length) {
        if (imgStr.includes(',')) {
          console.log('字符串中包含逗号');
          // 包含的进行slice(',') 分割

          this.imgViewArr = imgStr.split(',').map((item) => item.trim());
        } else {
          console.log('字符串中不包含逗号');
          this.imgViewArr = imgStr.split();
        }

        this.viewerVisible = true;
      } else {
        this.$message({
          message: '没有上传图片无法进行预览',
          type: 'warning'
        });
      }
    },
    removeComment (item) {
      console.log(item)

      const saveOrUpdate = delComment
      saveOrUpdate({

        id: item.id,

      })
        .then(async (msg) => {
          console.log(msg);
          this.loading = false;
          this.$message.success(msg.message);
          this.commentListFn(this.orderInfoData.id)

          const res = await circleInfo({ id: this.orderInfoData.id });

          console.log(res)
          if (res.code == 0) {



            this.orderInfoData = res.data
            this.loading = false
            if (res.data.img.includes(',')) {

              // 包含的进行slice(',') 分割

              this.orderInfoData.img = res.data.img.split(',').map((item) => item.trim());
            } else {
              if (res.data.img.length) {
                this.orderInfoData.img = res.data.img.split();
              } else {
                this.orderInfoData.img = []
              }

              console.log(this.orderInfoData.img)
              // this.orderInfoData.img = ['https://n8.weijuyunke.com/2024-06-28/a6939202406281049023506.png', 'https://n8.weijuyunke.com/2024-06-28/a6939202406281049023506.png', 'https://n8.weijuyunke.com/2024-06-28/a6939202406281049023506.png', 'https://n8.weijuyunke.com/2024-06-28/a6939202406281049023506.png', 'https://n8.weijuyunke.com/2024-06-28/a6939202406281049023506.png']
            }

            console.log(this.orderInfoData);
            if (this.orderInfoData.comment_number) {
              this.commentListFn(this.orderInfoData.id)
            } else {
              this.treeComentList = []
            }

          } else {
            this.$message.error(res.message)
          }

        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });


    },
    inputChange (e) {
      console.log(e)
      if (e == 3) {
        this.showTextarea = true
        this.status = e

      } else {

        this.showTextarea = false
        this.status = e
        this.remark = null


      }
    },
    // 关闭抽屉
    handleClose () {
      console.log('查看详情');
      this.$emit('update:drawerEditBoolen', false);
    },
    // 获取存储配置
    // async storageConfig () {
    //   const res = await storageConfigDetail();

    //   // console.log(res.storageMode);

    //   this.storageMode = res?.storageMode;
    // },
    // 关闭查看器
    closeViewer () {
      this.viewerVisible = false;
    },
    // 下载图片
    handleDownload (file) {
      window.open(file.url);
    },
    // 删除图片
    handleRemove () {
      this.fileList = [];
      this.imageUrl = '';
      this.form.img = this.imageUrl;
    },
    //  图片上传个数超过限制
    exceedFn () {
      this.$message({
        message: '图片上传已达到上限,请删除后再上传',
        type: 'warning'
      });
    },
    handlePictureCardPreview () {
      const imgStr = this.imageUrl;
      console.log(imgStr);
      if (imgStr.length) {
        if (imgStr.includes(',')) {
          console.log('字符串中包含逗号');
          // 包含的进行slice(',') 分割

          this.imgViewArr = imgStr.split(',').map((item) => item.trim());
        } else {
          console.log('字符串中不包含逗号');
          this.imgViewArr = imgStr.split();
        }

        this.viewerVisible = true;
      } else {
        this.$message({
          message: '没有上传图片无法进行预览',
          type: 'warning'
        });
      }
    },

    async handleFileChange (file) {

      this.aliFn(file)
    },
    // 阿里云
    async aliFn (file) {
      this.loading = true;

      let files = file.raw;

      console.log(files)
      // 构建请求参数
      let params = new FormData();
      console.log(params);

      //   params.append('signature', '132321313');
      params.append('file', files);
      console.log(params);

      //   let config = {
      //     headers: {
      //       'Access-Control-Allow-Origin': '*',
      //       'x-oss-callback-Authorization': getToken()
      //     }
      //   };

      const res = await upload(params);
      //   let result = await axios.post(res.host, param, config);
      //   let url = res.host + '/' + date + suffix;
      console.log(res);
      if (res.code == 0) {
        this.form.img = res.data.url
        this.loading = false;
      }
      //   this.form.categoryImages = url;
      //   this.imageUrl = url;
      //   this.loading = false;
    },

    /* 保存编辑 */
    save () {

      this.loading = true;

      const saveOrUpdate = updateCircleStatus
      saveOrUpdate({
        status: this.status,
        id: this.orderInfoData.id,
        remark: this.remark
      })
        .then((msg) => {
          console.log(msg);
          this.loading = false;
          this.$message.success(msg.message);

          this.handleClose();
          this.$emit('done');
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });



    },
    async commentListFn (id) {
      const res = await commentList({ page: 1, limit: 1000, circle_id: id });


      if (res.code == 0) {


        console.log(res.data.list)
        this.treeComentList = this.$util.toTreeData({
          data: res.data.list,
          idField: 'id',
          parentIdField: 'comment_id'

        })
        console.log(this.treeComentList)



      } else {
        this.$message.error(res.message)
      }
    }
    /* 更新visible */
  },
  watch: {
    drawerEditBoolen (visible) {
      this.$nextTick(async () => {
        if (visible) {
          console.log(this.data);
          if (this.data) {
            // this.orderInfoData = this.data
            this.loading = true
            const res = await circleInfo({ id: this.data.id });

            console.log(res)
            if (res.code == 0) {



              this.orderInfoData = res.data
              this.loading = false
              if (res.data.img.includes(',')) {

                // 包含的进行slice(',') 分割

                this.orderInfoData.img = res.data.img.split(',').map((item) => item.trim());
              } else {
                if (res.data.img.length) {
                  this.orderInfoData.img = res.data.img.split();
                } else {
                  this.orderInfoData.img = []
                }

                console.log(this.orderInfoData.img)
                // this.orderInfoData.img = ['https://n8.weijuyunke.com/2024-06-28/a6939202406281049023506.png', 'https://n8.weijuyunke.com/2024-06-28/a6939202406281049023506.png', 'https://n8.weijuyunke.com/2024-06-28/a6939202406281049023506.png', 'https://n8.weijuyunke.com/2024-06-28/a6939202406281049023506.png', 'https://n8.weijuyunke.com/2024-06-28/a6939202406281049023506.png']
              }

              console.log(this.orderInfoData);
              if (this.orderInfoData.comment_number) {
                this.commentListFn(this.orderInfoData.id)
              } else {
                this.treeComentList = []
              }

            } else {
              this.$message.error(res.message)
            }


            if (this.data.status == 3) {
              this.radio = 3
              this.remark = this.data.remark
              this.showTextarea = true
            } else {
              this.radio = 2
              this.status = 2
              this.remark = null
              this.showTextarea = false
            }

            this.isUpdate = true;
          } else {
            this.isUpdate = false;
          }
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
/*隐藏input（type=number)右边的小箭头*/
/deep/.delInput input::-webkit-outer-spin-button,
/deep/.delInput input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
/deep/.delInput input[type='number'] {
  -moz-appearance: textfield;
}
/deep/.el-textarea__inner {
  margin-top: 20px;
  min-height: 150px !important;
}
.allComments {
  margin-bottom: 15px;
  .commentator {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    margin-right: 5px;
  }

  .respondent {
    margin-right: 5px;
    width: 28px;
    height: 28px;
    border-radius: 50%;
  }

  .likeImg {
    margin-top: 29px;
    width: 18px;
    height: 18px;
  }

  .likeImgs {
    margin-top: 10px;
    width: 18px;
    height: 18px;
  }
}
.img {
  width: 42px;
  height: 42px;
}
.lifeImg {
  width: 90px;
  height: 90px;
  padding: 10px 10px 10px 0px;
}
.threePoints {
  width: 16px;
  height: 16px;
}
.foodsDiv div {
  margin-left: 20px;
  margin-top: 10px;
}
.height83 {
  height: 83vh;
}
.hLabel {
  margin: 10px 0 20px;
  font-weight: 700;
  &::before {
    content: '';
    display: inline-block;
    vertical-align: top;

    margin-right: 5px;
    height: 23px;
    width: 6px;
    background-color: #1e6ceb;
  }
}
.drawer__footer {
  display: flex;
  justify-content: flex-start;
  margin-left: 15px;
}
</style>
